package com.cuijianwei.myapp.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuijianwei.myapp.R;
import com.cuijianwei.myapp.fragment.LeftFragment;
import com.cuijianwei.myapp.fragment.RightFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends SlidingFragmentActivity implements View.OnClickListener {
    //展开左侧窗口的按钮
    private LinearLayout leftLl;
    //标题
    private TextView titleTv;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSlidingMenu(savedInstanceState);
        initView();
    }

    /**
     * 初始化左划窗口和右划窗口的内容
     *
     * @param savedInstanceState
     */
    private void initSlidingMenu(Bundle savedInstanceState) {
        if (savedInstanceState != null) {

        }
        //设置第一个滑动窗口
        setBehindContentView(R.layout.left);
        getSupportFragmentManager().beginTransaction().replace(R.id.left_fl, new LeftFragment()).commit();
        SlidingMenu sm = getSlidingMenu();
        //设置窗口的出现位置LEFT、RIGHT、LEFT_RIGHT
        sm.setMode(SlidingMenu.LEFT_RIGHT);
        //设置滑动阴影的宽度
        sm.setShadowWidthRes(R.dimen.shadow_width);
        //设置滑动菜单阴影的图像资源
        sm.setShadowDrawable(null);
        //设置滑动菜单视图的宽度
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        sm.setFadeDegree(0.35f);
        // 设置触摸屏幕的模式,TOUCHMODE_FULLSCREEN 为全屏,TOUCHMODE_MARGIN 为边缘
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        // 设置下方视图的在滚动时的缩放比例
        sm.setBehindScrollScale(0.0f);
        //设置第二个窗口
        sm.setSecondaryMenu(R.layout.right);
        getSupportFragmentManager().beginTransaction().replace(R.id.right_fl, new RightFragment()).commit();
    }

    private void initView() {
        leftLl = (LinearLayout) findViewById(R.id.left_ll);
        titleTv = (TextView) findViewById(R.id.title_tv);
        leftLl.setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.content_vp);
        getViews();
        viewPager.setAdapter(new MyViewPagerAdapter(getViews()));
    }

    private List<View> getViews() {
        List<View> views = new ArrayList<View>();
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View view1 = new View(MainActivity.this);
        view1.setLayoutParams(params);
        view1.setBackgroundColor(getResources().getColor(R.color.text_color1));
        View view2 = new View(MainActivity.this);
        view2.setLayoutParams(params);
        view2.setBackgroundColor(getResources().getColor(R.color.text_color2));
        View view3 = new View(MainActivity.this);
        view3.setLayoutParams(params);
        view3.setBackgroundColor(getResources().getColor(R.color.text_color3));
        views.add(view1);
        views.add(view2);
        views.add(view3);


        return views;
    }

    class MyViewPagerAdapter extends PagerAdapter {
        private List<View> list;

        public MyViewPagerAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position), 0);
            return list.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == (object);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_ll:
                toggle();
                break;
        }
    }
}
